package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import source.display.NewsAndWeatherView;
	import source.xml.*;

	[SWF(width="336", height="280", backgroundColor=0x444444)]

	public class NewsAndWeather extends Sprite
	{
		
		private var service:IXMLManager;
		private var service2:IXMLManager;
		private var view:NewsAndWeatherView;
		
		public function NewsAndWeather()
		{
			
			service = new XMLManager( 'http://rss.cnn.com/rss/cnn_world.rss' );
			service.addEventListener( XMLManager.STATE_CHANGE, handleStateChange );
			
			service2 = new XMLManager( 'http://weather.yahooapis.com/forecastrss?p=19711' );
			service2.addEventListener( XMLManager.STATE_CHANGE, handleStateChange );
			
			var timer:Timer = new Timer( 30000 );
			timer.addEventListener( TimerEvent.TIMER, refreshServices );
			timer.start();
			
			view = new NewsAndWeatherView();
			addChild( view );
			view.addService( service );
			view.addService( service2 );
			
		}
		
		private function handleStateChange( event:Event ):void
		{
			
			if( service.getState() == XMLManager.AVAILABLE )
			{
			
//				trace( service.getService() );
				
			} else {
				
				return;
				
			}
			
		}
		
		private function refreshServices( event:TimerEvent ):void
		{
			
//			trace( 'About to refresh services' );	
			service.refresh();
			service2.refresh();
			
		}
		
	}
}
